package com.czg.app;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author czg
 */
public class ConfigUtil {

    private static final String configRootDir = System.getProperty("user.home") + "/.jsonSchema2LombokPojo";
    private static final String configFile = System.getProperty("user.home") + "/.jsonSchema2LombokPojo" + "/app.json";
    private static final String KEY_VALUE = "keyValue";
    private static final String KEY = "key";
    private static final String VALUE = "value";

    static {
        FileUtil.mkdir(configRootDir);
        JSONObject configJSONObject = FileUtil.exist(configFile) ? JSON.parseObject(FileUtil.readUtf8String(configFile)) : new JSONObject();
        if (configJSONObject.getJSONArray(KEY_VALUE) == null) {
            configJSONObject.put(KEY_VALUE, new JSONArray());
        }
        FileUtil.writeUtf8String(configJSONObject.toJSONString(), configFile);
    }

    private final JSONObject rootJson = JSON.parseObject(FileUtil.readUtf8String(configFile));

    private final JSONArray keyValueJSONArray = rootJson.getJSONArray(KEY_VALUE);

    private void save() {
        FileUtil.writeUtf8String(rootJson.toJSONString(), configFile);
    }

    public boolean getBool(String key) {
        String value = getValue(key, null);
        return "true".equals(value);
    }

    public String getValue(String key) {
        return getValue(key, null);
    }

    public String getValue(String key, String defaultValue) {
        return getBasicValue(keyValueJSONArray, KEY, key, VALUE, defaultValue);
    }

    public void setValue(String key, boolean value) {
        setValue(key, value ? "true" : "false");
    }

    public void setValue(String key, String value) {
        setBasicValue(keyValueJSONArray, KEY, key, VALUE, value);
    }

    private String getBasicValue(JSONArray jsonArray, String keyName, String key, String valueName, String defaultValue) {
        if (jsonArray == null) {
            return null;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (jsonObject.getString(keyName).equals(key)) {
                return jsonObject.getString(valueName);
            }
        }
        return defaultValue;
    }

    public void setBasicValue(JSONArray jsonArray, String keyName, String key, String valueName, String value) {
        if (jsonArray == null) {
            return;
        }
        boolean found = false;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (jsonObject.getString(keyName).equals(key)) {
                jsonObject.put(valueName, value);
                found = true;
            }
        }
        if (!found) {
            jsonArray.add(new JSONObject() {{
                put(keyName, key);
                put(valueName, value);
            }});
        }
        save();
    }

    public boolean existFile() {
        return FileUtil.exist(configFile);
    }
}
